<?php
namespace BoxSns\Logic;
class TopicLogic extends \LogicController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->topic = BoxModel("SnsTopic");

    }

    /*******创建帖子*****/
    public function add($row)
    {
 
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        if ($error['error'] > 0) {
            return $error;
        }
        /****是否定义可以发此类型的帖子**/
        //$topic_type=dict("topic_type");
//		if(!$topic_type[$row['type']]){
//			return array(
//				'error'	=>3,
//				'msg'	=>$row['type'].'未在字典中定义，不充许发布此类型的帖子',
//			);
//		}
        $type = $row['type'];
        $pid = $row['pid'];
        /***如果是群组，发帖验证***/
        if ($type == "group") {
            $error = Logic("BoxSns", "Group")->checkMake($this->user_id, $pid);
            if ($error['error'] > 0) {
                return $error;
            }
        }
							 //dump($row);exit();
        $row['user_id'] = $this->user_id;
        /*******内容处理******/
								$row['title']=AtStr(strip_tags(stripslashes(htmlspecialchars_decode($row['title'])),dict('allow_tags')));
								$row['content']=strip_tags(stripslashes(htmlspecialchars_decode($row['content'])),dict('allow_tags'));
      
						  if(trim(strip_tags($row['content'],"<img>"))=="" && trim(strip_tags($row['title'],"<img>"))==""){
									 return array('error'=>3,'msg'=>'提交内容不能为空');
								} 
								
								
						 // $user_ids = Logic("BoxSns", "Noti")->At($title); //需要@提醒的用户ID
       // $title = autolink($title);                    //加上超链接
       // $title = AtStr($title);                        //有@加上链接
        //$row['title'] = $title;
        $topic_id = $this->topic->create_topic($row);
        return array(
            'error' => 0,
            'msg' => $this->topic->load($topic_id),
        );
    }

    /*******删除权限**/
    function act_del($topic_id)
    {
        $data = $this->topic->load($topic_id);
        if (!$data) {
            return array(
                'error' => 2,
                'msg' => '帖子不存在',
            );
        }
        if ($data['is_show'] == 0) {
            return array(
                'error' => 3,
                'msg' => '帖子已经删除',
            );
        }
        /*******版主和发帖人可以删除****/
        if ($data['pidData']['user_id'] != $this->user_id && $data['user_id'] != $this->user_id) {
            return array(
                'error' => 1,
                'msg' => '你无权操作',
            );
        }
        return true;
    }

    /*****删除帖子$param=array('topic_id'=>$topic_id);*****/
    public function del($param)
    {
        $topic_id = $param['topic_id'];
        $r = $this->act_del($topic_id);
        if ($r === true) {
            $id = $this->topic->del($topic_id);
            return $id;
        } else {
            return $r;
        }
    }

    /*****赞一个帖子,带一个参数topic_id***/
    public function goods($parms)
    {
        $topic_id = $parms['topic_id'];
        $data = $this->topic->load($topic_id);
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        $type = $data['type'];
        if ($error['error'] > 0) {
            return $error;
        }
        if (!$type) {
            return array(
                'error' => 5,
                'msg' => '无法识别type',
            );
        }
        $where = array(
            'topic_id' => $topic_id,
            'user_id' => $this->user_id,
        );
        $model = BoxModel("sns_topic_goods");
        $r = $model->where($where)->find();
        if ($r) {
            return array(
                'error' => 6,
                'msg' => '你已经赞过了',
            );
        }
        $r = $model->add($where);
        if ($r) {
            BoxModel("sns_topic")->statistics($topic_id, "good_count", "setInc");
            return array(
                'error' => 0,
                'msg' => '操作成功',
            );

        } else {
            return array(
                'error' => 7,
                'msg' => '数据保存失败',
            );
        }


    }


    /****************生成html代码*******/
    public function emotion_html()
    {
        $Html = S("emotion_html");
        if (!$Html) {
            $Html = "";
            $data = $this->emotion();
            $emotion_data = $data['msg']['Variables']['smilies'];
            //dump($emotion_data);
            $tabHtml = '';
            $conHtml = '';
            $j = 0;
            $exprePath = "/Public/Js/smiley/";
            foreach ($emotion_data as $k => $expreCode) {
                $conHtml .= ' <div class="swiper-slide">';
                foreach ($expreCode as $i => $one) {
                    $conHtml .= '<a class="expre" title="' . $one['code'] . '" href="#"><img class="smiley" width="80%" src="' . $exprePath . $one['image'] . '"></a>';
                }
                $conHtml .= '</div>';
            }
            // $$('.expreList .expreCon').html(conHtml);
            $Html .= '<div class="swiper-container emotion" >';
            $Html .= '  <div class="swiper-wrapper expreCon">' . $conHtml . '</div>';
            $Html .= ' <div class="swiper-pagination swiper-pagination-emotion"></div>';
            $Html .= '</div>';
            S("emotion_html", $Html);
        }
        echo $Html;
    }

    /****表情****/
    public function emotion()
    {
        $data = S('emotion_data');
        if (!$data) {
            $path = BOX_PATH . "/Public/Js/smiley";//表情的路径
            $data = getFile($path);
            $emotion_data = array();
            $limit = 27;
            foreach ($data as $k => $one) {
                $k++;
                $one = trim(str_replace($path, "", $one), "\\");
                $one = str_replace("\\", "/", $one);
                $one1 = explode("/", $one);
                $code = "{:" . $k . "}";
                $page = ceil(($k / $limit));
                $emotion_data[$page][] = array(
                    'code' => $code,
                    'image' => $one,
                );
                $replace_data['code'][] = $code;
                $replace_data['smilies'][] = '<img src="/Public/Js/smiley/' . $one . '" border="0" class="">';
            }
            $data = array(
                'Version' => 4,
                'Charset' => "UTF-8",
                'Variables' => array('smilies' => $emotion_data),
                'replace_data' => $replace_data,
            );
            S('emotion_data', $data);
        }
        return array(
            'error' => 0,
            'msg' => $data,
        );
    }

    /*

返回值(纯文本) 如：DB|0|1051301|1|0
             DISCUZUPLOAD为固定输出
             第一个0	代表报错信息，0为正常，非零为出错，1,上传文件为空，2，写入数据库失败，3图片过大。
             第二个1051301	代表成功上传图片的附件aid
             第三个1	代表是否是图片
             第四个0	代表导致出错的上传文件容量的尺寸，比如1024000,则限制1024000字节以下的
$param[path]			 
*/
    public function Upload($param)
    {
        $path = $param['path'] ? $param['path'] : "topic";
        $field = 'filedata';
        if (empty($_FILES) || !$_FILES[$field]['name']) {
            $str = 'DB|1|309|1|0';
        } else {
            $r = $this->_upload($path . "/");
            if ($r['filedata']['name']) {
                $url = $r['filedata']['savepath'] . $r['filedata']['savename'];
                $str = "DB|0|{$url}|1|0";
            } else {
                $str = 'DB|2|0|1|0';
            }
        }
        return array(
            'error' => 0,
            'msg' => $str,
        );
    }


}